package com.shahe.GirlsDay;

import java.util.Calendar;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.shahe.GirlsDay.util.BasicInfo;
import com.shahe.GirlsDay.util.ContactPerson;
import com.shahe.GirlsDay.util.HttpHelper;
import com.shahe.GirlsDay.util.StaticClass;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Sarah on 5/06/2014.
 */
public class HomeActivity extends Activity {
	/**
	 * Called when the activity is first created.
	 */
	int restDays = 0;

	private Calendar calendar;
	private TextView nextPeriodText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage);

		final Button logBtn = (Button) findViewById(R.id.btn_log);
		final Button calendarBtn = (Button) findViewById(R.id.btn_calendar);
		final Button chartBtn = (Button) findViewById(R.id.btn_chart);
		final Button settingsBtn = (Button) findViewById(R.id.btn_settings);

		calendar = Calendar.getInstance();
		StaticClass.currentDate = calendar.get(Calendar.YEAR) + "-"
				+ (calendar.get(Calendar.MONTH) + 1) + "-"
				+ calendar.get(Calendar.DATE);

		int nowDate = calendar.get(Calendar.YEAR)*365 + (calendar.get(Calendar.MONTH) + 1)*30 + calendar.get(Calendar.DATE);

		if (StaticClass.myInfo.getUserid() > 0) {
			String json = null;
			try {
				json = new getAllBasicTask().execute(
						StaticClass.myInfo.getUserid() + "").get();
			} catch (InterruptedException | ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (json.length() > 2) {
				JSONArray jsonArray = new JSONArray();
				try {
					jsonArray = new JSONArray(json);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject2 = new JSONObject();

					try {
						jsonObject2 = jsonArray.getJSONObject(i);
						//warring(jsonObject2.toString());
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					BasicInfo basicInfo = null;

					try {
						basicInfo = new BasicInfo(jsonObject2);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					StaticClass.dayList.add(basicInfo.getAdddate());
					StaticClass.DayInfoList.add(basicInfo);

				}
			}
		}
		
		nextPeriodText = (TextView) findViewById(R.id.tv_nextPeriod);

		if (StaticClass.DayInfoList.size() > 0) {
			int a = 0;
			int b = 0;
			int c = 0;
			for (int i = 0; i < StaticClass.dayList.size(); i++) {
				if (StaticClass.DayInfoList.get(i).getDaytype().equals("1")) {
					if (a < Integer.valueOf(StaticClass.dayList.get(i).split(
							"-")[0])) {
						a = Integer.valueOf(StaticClass.dayList.get(i).split(
								"-")[0]);
						b = Integer.valueOf(StaticClass.dayList.get(i).split(
								"-")[1]);
						c =Integer.valueOf(StaticClass.dayList.get(i).split(
								"-")[2]);
					}
					else if(a == Integer.valueOf(StaticClass.dayList.get(i).split(
							"-")[0])){
						b = Integer.valueOf(StaticClass.dayList.get(i).split(
								"-")[1]);
						c = Integer.valueOf(StaticClass.dayList.get(i).split(
								"-")[2]);						
					}
					else if( c == Integer.valueOf(StaticClass.dayList.get(i).split(
							"-")[2])){
						c = Integer.valueOf(StaticClass.dayList.get(i).split(
								"-")[2]);
					}
					
					if ((a*365+b*30+c+35) >= nowDate) {
						restDays = (a*365+b*30+c+ 35 ) - nowDate;
						nextPeriodText.setText("Next Period: " + restDays + " Days ");
					}

					// b = i;
				}
			}

			

			
		}

		Toast.makeText(getApplicationContext(), StaticClass.currentDate,
				Toast.LENGTH_SHORT).show();

		logBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this, LogActivity.class);
				startActivity(intent);
			}
		});

		calendarBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this,
						CalendarActivity.class);
				startActivity(intent);
			}
		});

		chartBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this,
						ChartActivity.class);
				startActivity(intent);
			}
		});

		settingsBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this,
						SettingsActivity.class);
				startActivity(intent);
			}
		});

	}

	private class getAllBasicTask extends AsyncTask<String, String, String> {

		protected String doInBackground(String... strings) {
			return HttpHelper.getBasic(strings[0]);
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}

	public void warring(String str) {
		Toast.makeText(this, str, Toast.LENGTH_LONG).show();
	}
}